import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'
import About from '../views/About.vue'
import Profile from '../components/Profile.vue'
// 下面路由懒加载方式导入
const HomeNews = () => import('../components/HomeNews.vue')
const HomeMsg = () => import('../components/HomeMsg.vue')

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/profile/',
    component: Profile,
    meta: {
      title: '简介'
    }
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta: {
      title: '主页'
    },
    children: [
      {
        path: '',
        component: HomeNews
      },
      {
        path: 'news',
        component: HomeNews,
        meta: {
          title: '新闻'
        }
      },
      {
        path: 'msg',
        component: HomeMsg,
        meta: {
          title: '消息'
        }
      }
    ]
  },
  {
    path: '/about',
    name: 'About',
    component: About,
    meta: {
      title: '关于'
    }
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
router.beforeEach((to, form, next) => {
  if (to.matched[0].name === 'User') {	// 如果用户单击user链接，路由将被拦截    console.log('拦截')
  } else {								// 如果用户不是单击user链接
    document.title = to.meta.title		// 读取路由元信息，并修改网页标题
    next() 								// 跳转到指定页面
  }
})
export default router